package  Classes.LevelManagement.Levels
{
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Classes.LevelManagement.Level_Base;
	import Classes.Utils.Input;
	import flash.display.MovieClip;
	import Classes.Main
	import Classes.Utils.PixelPerfectCollisionDetection;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.sampler.NewObjectSample;
	import flash.ui.Keyboard;
	import flash.ui.Mouse;

	/**
	 * ...
	 * @author Rodrigo Forti & Rafael Bringel
	 */
	public class Level4 extends Level_Base
	{			

		public var world:b2World;
		public var wheelArray:Array;
		
		public var scaleFactor: int = 20;
		
		public function Level1() 
		{
			
		}
		
		public override function start():void
		{
			var gravity:b2Vec2 = new b2Vec2(0, 10);
			world = new b2World(gravity, true);
			
			wheelArray = new Array();
			
			Main.getStage().addEventListener(MouseEvent.MOUSE_DOWN, click);
			
			createWheels(100);
					
			var groundBodyDef:b2BodyDef = new b2BodyDef();
			groundBodyDef.position.Set(0, Main.getStage().stage.stageHeight / scaleFactor);
			var groundBody:b2Body = world.CreateBody(groundBodyDef);
			var groundShape:b2PolygonShape = new b2PolygonShape();
			groundShape.SetAsBox(Main.getStage().stage.stageWidth, 1);
			var groundFixtureDef:b2FixtureDef = new b2FixtureDef();
			groundFixtureDef.shape = groundShape;
			var groundFixture:b2Fixture = groundBody.CreateFixture(groundFixtureDef);
		 
			var rightWallBodyDef:b2BodyDef = new b2BodyDef();
			rightWallBodyDef.position.Set(Main.getStage().stage.stageWidth / scaleFactor, 0);
			var rightWallBody:b2Body = world.CreateBody(rightWallBodyDef);
			var rightWallShape:b2PolygonShape = new b2PolygonShape();
			rightWallShape.SetAsBox(1, Main.getStage().stage.stageHeight);
			var rightWallFixtureDef:b2FixtureDef = new b2FixtureDef();
			rightWallFixtureDef.shape = rightWallShape;
			var rightWallFixture:b2Fixture = rightWallBody.CreateFixture(rightWallFixtureDef);
		 
			var leftWallBodyDef:b2BodyDef = new b2BodyDef();
			leftWallBodyDef.position.Set(0, 0);
			var leftWallBody:b2Body = world.CreateBody(leftWallBodyDef);
			var leftWallShape:b2PolygonShape = new b2PolygonShape();
			leftWallShape.SetAsBox(1, Main.getStage().stage.stageHeight);
			var leftWallFixtureDef:b2FixtureDef = new b2FixtureDef();
			leftWallFixtureDef.shape = leftWallShape;
			var leftWallFixture:b2Fixture = leftWallBody.CreateFixture(leftWallFixtureDef);
		 
			var ceilingBodyDef:b2BodyDef = new b2BodyDef();
			ceilingBodyDef.position.Set(0, 0);
			var ceilingBody:b2Body = world.CreateBody(ceilingBodyDef);
			var ceilingShape:b2PolygonShape = new b2PolygonShape();
			ceilingShape.SetAsBox(Main.getStage().stage.stageWidth, 1);
			var ceilingFixtureDef:b2FixtureDef = new b2FixtureDef();
			ceilingFixtureDef.shape = ceilingShape;
			var ceilingFixture:b2Fixture = ceilingBody.CreateFixture(ceilingFixtureDef);
		 
		}
		
		public override function update():void
		{
			world.Step(0.025, 10, 10);
			
			Main.getInstance().MC_tela.graphics.clear();
			Main.getInstance().MC_tela.graphics.lineStyle(3, 0xff0000);
			
			for each (var wheelBody:b2Body in wheelArray)
			{
				Main.getInstance().MC_tela.graphics.drawCircle
				(
					wheelBody.GetPosition().x * scaleFactor, 
					wheelBody.GetPosition().y * scaleFactor, 
					(wheelBody.GetFixtureList().GetShape() as b2CircleShape).GetRadius() * scaleFactor
				);
			}
			
			
		}
		
		public function createWheels(amount: int):void
		{
	
			for (var i:int = 0; i < amount; i++)
			{
				var wheelBodyDef:b2BodyDef = new b2BodyDef();
				wheelBodyDef.position.Set(randomNumber(30, (Main.getStage().stageWidth - 30) / scaleFactor), randomNumber(30, Main.getStage().stageHeight - 30) / scaleFactor);
				wheelBodyDef.type = b2Body.b2_dynamicBody;
				var wheelBody = world.CreateBody(wheelBodyDef);
				var circleShape:b2CircleShape = new b2CircleShape(Math.random() * 0.5  + 0.1);
				var wheelFixtureDef:b2FixtureDef = new b2FixtureDef();
				wheelFixtureDef.shape = circleShape;
				wheelFixtureDef.restitution = (Math.random() * 0.5) + 0.5;
				wheelFixtureDef.friction = (Math.random() * 1.0);
				wheelFixtureDef.density = Math.random() * 20;
				var wheelFixture:b2Fixture = wheelBody.CreateFixture(wheelFixtureDef);
				
				var startingVelocity:b2Vec2 = new b2Vec2(randomNumber(-50, 50), randomNumber(-50, 50));
				wheelBody.SetLinearVelocity(startingVelocity);
				
				wheelArray.push(wheelBody);
			}	
		}
		
		public function click(e:MouseEvent):void
		{	
			for (var i:int = 0; i < wheelArray.length; i++)
			{
				var dist = Math.sqrt(Math.pow(mouseX - wheelArray[i].GetPosition().x * scaleFactor, 2) + Math.pow(mouseY - wheelArray[i].GetPosition().y * scaleFactor, 2));
				var distInvertida = (((dist / scaleFactor) - 46) * -1);
			
				var some:b2Body;
				
				
				var angleRadian:Number = Math.atan2((mouseY - wheelArray[i].GetPosition().y * scaleFactor), (mouseX - wheelArray[i].GetPosition().x * scaleFactor));
				var angleDegree:Number = angleRadian * 180 / Math.PI;
				

				if (angleDegree < 0)
				{
					angleDegree = angleDegree + 360;
				}
				
				var startingVelocity:b2Vec2 = new b2Vec2(-Math.cos(angleRadian) * distInvertida, -Math.sin(angleRadian) * distInvertida);
				wheelArray[i].SetLinearVelocity(startingVelocity);
				
				//world.SetGravity(new b2Vec2(Math.cos(((angulo) * Math.PI) / 180) * 5, Math.sin(((angulo) * Math.PI) / 180) * 5));
			}
		}
		
		function randomNumber(low:Number=0, high:Number=1):Number
		{
		  return Math.floor(Math.random() * (1+high-low)) + low;
		}
	}

}